class Api::V1::ProductProcessesController < Api::V1::BaseController
  before_action :authenticate_request!

  def index
    @product_processes = apply_scopes(ProductProcess).all
    @product_processes = paginate(@product_processes)
    render json: @product_processes, meta: paginate_meta(@product_processes)
  end

  def batch_create
    begin
      pps = []
      create_params[:attributes].each do |pp|
        pps << ProductProcess.new(pp)
      end

      ProductProcess.import! pps
      render status: 201
    rescue RecordInvalid
      render status: 403
    rescue
      render status: 403
    end
  end

  def create
    begin
      @product_process = ProductProcess.new(create_params[:attributes])
      authorize @product_process
      @product_process.save!
      render json: @role, status: 201
    rescue ActiveRecord::StatementInvalid
      @product_process.error.add(:exec_error, 'Contact Administrator')
      error_render(@role, 403)
    rescue => err
      error_render(err.record, 403)
    end
  end

  private
  def create_params
    params.require(:data).permit(:type, :id, attributes: [])
  end
end
